<template>
	<view class="page">
		<view class="head">
			<Search @search="search" ref="search"></Search>
			<view>
				<AdImagesSwiper :adImagesList="adsImg"></AdImagesSwiper>
			</view>
		</view>
		<view class="body">
			<view class="information" >
				<view style="height: 100rpx;" v-if="screenMenuIsShow">
					<view class="information-class" @click="screenMenuIsFixed" :class="screenIsFixed">
						<Screen :screenList="screenList" @change="screenSelectItem"></Screen>
					</view>
				</view>
				<view class="infomation-list">
					<NewsPaperInfoItem :infoList="infoList"></NewsPaperInfoItem>
				</view>
			</view>
		</view>
		<view class="bottem">
			<!-- <BackHome v-if="backTopValue"></BackHome> -->
			<BackHome :backTopIsShow="backTopValue"></BackHome>
		</view>
	</view>
</template>

<script>
	import Search from "@/components/Search/Search.vue"
	import NewsPaperInfoItem from "@/components/NewsPaperInfoItem/NewsPaperInfoItem.vue"
	import Screen from "@/components/Screen/Screen.vue"
	import BackHome from "@/components/FloatButton/BackHome.vue"
	import AdImagesSwiper from "@/components/AdImagesSwiper/AdImagesSwiper.vue"
	export default {
		data() {
			return {
				adsImg:[],
				infoClassid:0,
				infoClassType:0,
				infoNewsId:0,
				infoList:[],
				// tempscreenList:[], //临时存储下拉菜单对象数据
				screenList:[],//下拉菜单对象
				screenIsFixed: '', //下拉是否固定
				params:{},
				currentPage: 1, // 默认加载第二页
				allPage: 1, // 所有页面
				backTopValue: false, // 判断是否显示悬浮返顶按钮
				screenMenuIsShow:true ,//当分类为0时不显示下拉筛选菜单
				defaultParams:{}, //设置默认的参数对象
				searchKey: '' // 搜索关键字
			}
		},
		async onLoad(option) {  //option为object类型，会序列化上个页面传递的参数
			await this.$onLaunched;
			this.params = JSON.parse(decodeURIComponent(option.parameter)); //打印出上个页面传递的参数。
			console.log(this.params)
			this.infoClassId = this.params.ID
			this.infoClassType = this.params.Type
			// console.log(typeof(Number.parseInt(this.params.NewsId)))
			this.infoNewsId = Number.parseInt(this.params.NewsId)
			if(this.infoClassType == 0){
				this.screenMenuIsShow = false
			}
			uni.setNavigationBarTitle({
			    title: this.params.ClassName
			});
			this.defaultParams = {cid:getApp().globalData.cityId,id:this.infoClassId,type:this.infoClassType,newid: this.infoNewsId,openid:getApp().globalData.openid}
			let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage})
			this.getNewsPaperInfoList(paramsObject)
		},
		onReachBottom(){
			this.currentPage++
			if(this.currentPage <= this.allPage){
				if(this.searchKey){
					let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage, search: this.searchKey, lanmuID:this.infoClassId})
					this.searchMore(paramsObject)
				} else {
					let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage})
					this.getNewsPaperInfoListMore(paramsObject)
				}
			}
		},
		onPullDownRefresh(){
			this.screenList = [] //让子组件重新加载数据
			this.infoList = [] //让子组件重新加载数据
			this.currentPage = 1 // 默认加载第二页
			this.allPage = 1 // 所有页面
			this.searchKey = '' // 设置关键字为空
			this.$refs.search.searchKey = '' // 设置子组件关键字为空
			if(this.infoClassType == 0){
				this.screenMenuIsShow = false
			}
			let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage})
			this.getNewsPaperInfoList(paramsObject)
			// this.$forceUpdate()
		},
		onPageScroll(e){
			if(e.scrollTop>205){
				this.screenIsFixed = 'is-fixed'
				this.backTopValue = true
			}else{
				this.screenIsFixed = '';
				this.backTopValue = false
			}
		},
		methods: {
			screenSelectItem(e){
			    console.log(e)
				this.currentPage = 1
				this.allPage = 1
				let paramsObject = {cid:getApp().globalData.cityId,id:this.infoClassId,type:this.infoClassType,newid: this.infoNewsId,openid:getApp().globalData.openid,page:this.currentPage}
				e.forEach(el=>{
					paramsObject = Object.assign(paramsObject, JSON.parse('{"'+el.classId+'": '+el.ID+'}'))
				})
				paramsObject = Object.assign(paramsObject, JSON.parse('{"XLID": '+ paramsObject.XLID1 +'}'))
				console.log(paramsObject)
				this.infoList = []
				this.defaultParams = paramsObject
				// this.tempscreenList = []
				// this.screenList = []//下拉菜单对象
				this.getNewsPaperInfoListMore(paramsObject)
			},
			getNewsPaperInfoList(params){
				this.$api.getNewsPaperInfoList(params)
				.then(res => {
					console.log(res.data)
					// this.bannerImgUrl = this.$getImgUrl(res.data.KBPic)
					// this.columnSwiper = res.data.LanMu // 栏目列表
					// this.vpiImageUrl = this.$imgUrl + res.data.VIPPic  // vip图片
					this.allPage = res.data.Allpage// 所有页面
					this.infoList = res.data.XXList
					this.adsImg = res.data.AdsImg // 广告图片
					this.indexScreenXlList1 = res.data.XLList
					
					// 组合下拉菜单数据对象 开始
					let tempscreenList = []
					if(this.infoClassType == 1){
						tempscreenList.push({classId:'XLID1',className:res.data['Name'],data:res.data['XLList']})
					}else{
						for(let i=1;i<=this.infoClassType;i++)
						{
							tempscreenList.push({classId:'XLID'+ i,className:res.data['XLName'+i],data:res.data['XLList'+i]})
						}
					}
					this.screenList = tempscreenList
					
					this.$forceUpdate()
					uni.stopPullDownRefresh()
					// 组合下拉菜单数据对象 结束
				})
			},
			getNewsPaperInfoListMore(params){
				// this.onLoading(1500)
				this.$api.getNewsPaperInfoList(params)
				.then(res =>{
					this.infoList = [...this.infoList, ...res.data.XXList]
					this.currentPage = res.data.CurrentPage
					this.allPage = res.data.Allpage// 所有页面
					this.$forceUpdate()
				})
				
			},
			screenMenuIsFixed(){
				uni.pageScrollTo({
				    duration:0,
				    scrollTop:205
				})
			},
			onLoading(time){
				uni.showToast({
				  title: "加载中...",
				  icon: "loading",
				  duration: time,
				})
			},
			search(key){
				this.searchKey = key
				let paramsObject = Object.assign(this.defaultParams, {page:this.currentPage, search: this.searchKey, lanmuID:this.infoClassId})
				this.currentPage = 1
				this.allPage = 1
				this.infoList = []
				this.$api.getNewsPaperSearchInfoList(paramsObject).then(res => {
					console.log(res)
					this.infoList = [...this.infoList, ...res.data.SearchList]
					this.currentPage = res.data.CurrentPage
					this.allPage = res.data.Allpage// 所有页面
					this.$forceUpdate()
				})
			},
			searchMore(params){
				
				this.$api.getNewsPaperSearchInfoList(params).then(res => {
					console.log(res)
					this.infoList = [...this.infoList, ...res.data.SearchList]
					this.currentPage = res.data.CurrentPage
					this.allPage = res.data.Allpage// 所有页面
					this.$forceUpdate()
				})
			},
		},
		components:{
			Search,
			NewsPaperInfoItem,
			Screen,
			BackHome,
			AdImagesSwiper,
		}
	}
</script>

<style scoped>
@import url('newsPaperList.css');
</style>
